package com.spzx.product.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.api.domain.vo.Category;
import com.spzx.product.domain.vo.CategoryExcelVo;
import com.spzx.product.help.CategoryHelper;
import com.spzx.product.listeners.MyCategoryExcelListener;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Resource
    CategoryMapper categoryMapper;

    @Override
    public List<Category> treeSelect(Long parentId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);
        categoryList.stream().filter(category -> {
            queryWrapper.clear();
            queryWrapper.eq("parent_id", category.getId());
            Long count = baseMapper.selectCount(queryWrapper);
            if (count > 0) {
                category.setHasChildren(true);
            } else {
                category.setHasChildren(false);
            }
            return true;
        }).collect(Collectors.toList());
        return categoryList;
    }

    @Override
    public List<Long> getParentIds(Long category3Id) {
        List<Category> CategoryCar = new ArrayList<>();
        CategoryCar = getCategoryParentId(category3Id, CategoryCar);
        List<Long> ids = new ArrayList<>();
        for (Category category : CategoryCar
        ) {
            ids.add(0, category.getId());

        }

        return ids;
    }

    @SneakyThrows
    @Override
    public void exportData(HttpServletResponse response) {
        ServletOutputStream outputStream = response.getOutputStream();
        List<Category> selectList = baseMapper.selectList(null);
        List<CategoryExcelVo> categoryExcelVos = selectList.stream().map(category -> {
            CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
            BeanUtils.copyProperties(category, categoryExcelVo);
            return categoryExcelVo;
        }).collect(Collectors.toList());
        EasyExcel.write(outputStream, CategoryExcelVo.class).sheet("分类信息").doWrite(categoryExcelVos);
    }

    @Override
    @SneakyThrows
    public void importData(MultipartFile file) {
        InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream, CategoryExcelVo.class, new MyCategoryExcelListener(this)).sheet("分类信息").doRead();
    }

    @Override
    public List<CategoryVo> getOneCategory() {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0));
        return categoryList.stream().map(item ->{
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVo> tree() {
        List<Category> categoriesList = baseMapper.selectList(null);
        List<CategoryVo>voList=categoriesList.stream().map(item->{
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return  categoryVo;
        }).collect(Collectors.toList());
        return CategoryHelper.buildTree(voList);
    }

    private List<Category> getCategoryParentId(Long categoryId, List<Category> categoryCar) {
        while (categoryId > 0) {
            Category category = baseMapper.selectById(categoryId);
            if (category != null) {
                categoryCar.add(category);
            }
            Long parentId = category.getParentId();
            return getCategoryParentId(parentId, categoryCar);
        }

        return categoryCar;
    }
}
